package com.zjw.core.algorithm;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) {
        int[] num = new int[10];
        Map<Integer,Integer> topMap = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            num[i] = (int) (Math.random()* 10);
            topMap.put(i,num[i]);
            System.out.print(num[i] + " ");
        }
        HashMap<Integer, Integer> finalOut = (HashMap<Integer, Integer>) sortMapByValues(topMap);
        for (Map.Entry entry:finalOut.entrySet()) {
            System.out.println("排序后的值："+entry.getValue());
        }
    }

    public static <K extends Comparable, V extends Comparable> Map<K, V> sortMapByValues(Map<K, V> aMap) {
        HashMap<K, V> finalOut = new LinkedHashMap<>();
        aMap.entrySet()
                .stream()
                .sorted((p1, p2) -> p2.getValue().compareTo(p1.getValue()))
                .collect(Collectors.toList()).forEach(ele -> finalOut.put(ele.getKey(), ele.getValue()));
        return finalOut;
    }
}
